import React, { useEffect, useState } from 'react'
import { message, Modal, Form, Input } from 'antd';
import { UserType_Add, Type_Add ,Variety_Add,Specification_Add} from '../service';
const AddModel = ({ isModalOpen, setIsModalOpen,UserTypeUpdate, AddType,SpecificationUpdate, TypeUpdate ,VarietyUpdate}) => {

    const [Label, setLabel] = useState({
        label: "使用人类型",
        name: "user_type"
    });


    useEffect(() => {

        if (AddType == "新增使用人类型") {

            setLabel({
                label: "使用人类型",
                name: "user_type"
            });
        }

        if (AddType == "新增柜种类") {
            setLabel({
                label: "柜锁类型名称",
                name: "type_name"
            });
        }
        if (AddType == "新增柜子分类") {
            setLabel({
                label: "柜子分类名称",
                name: "product_class"
            });
        }

        if(AddType == "新增锁规格"){
            setLabel({
                label: "锁规格名称",
                name: "spec_name"
            });
        }

    }, [])

    console.log(AddType)
    const [form] = Form.useForm();

    const handleSubmit = () => {
        form.validateFields().then(async values => {

            if (AddType == "新增使用人类型") {
                let res = await UserType_Add({ ...values, user_type_code: getRandomCode() })
                if (res.code == 0) {
                    message.success("添加成功")
                    UserTypeUpdate()
                    setIsModalOpen(false)

                } else {
                    message.error(res.msg)
                }
            }

            if (AddType == "新增柜种类") {
                let res = await Type_Add({ ...values, type_code:getRandomCode() })
                if (res.code == 0) {
                    message.success("添加成功")
                    setIsModalOpen(false)
                    TypeUpdate()
                } else {
                    message.error(res.msg)
                }
            }
            if (AddType == "新增柜子分类") {
                let res = await Variety_Add({ ...values, product_code: getRandomCode() })
                if (res.code == 0) {
                    message.success("添加成功")
                    setIsModalOpen(false)
                    VarietyUpdate()
                } else {
                    message.error(res.msg)
                }
            }
            if (AddType == "新增锁规格") {
                let res = await Specification_Add({ ...values, spec_code: getRandomCode() })
                if (res.code == 0) {
                    message.success("添加成功")
                    setIsModalOpen(false)
                    SpecificationUpdate()
                } else {
                    message.error(res.msg)
                }
            }

        });
    };

    function getRandomCode() {
        // 生成 4 位随机数字
        const digits = Math.floor(1000 + Math.random() * 9000).toString(); // 1000-9999

        // 生成 2 位小写字母（a-z）
        const letters = Array.from({ length: 2 }, () =>
            String.fromCharCode(97 + Math.floor(Math.random() * 26))
        ).join('');

        return digits + letters;
    }



    return (
        <div>

            <Modal
                title={AddType}
                closable={{ 'aria-label': 'Custom Close Button' }}
                visible={isModalOpen}
                onCancel={() => setIsModalOpen(false)}
                onOk={handleSubmit}
            >
                <Form
                    name="basic"
                    labelCol={{ span: 7 }}
                    wrapperCol={{ span: 16 }}
                    style={{ maxWidth: 600 }}
                    form={form}
                    autoComplete="off"
                >
                    <Form.Item
                        label={Label.label}
                        name={Label.name}
                        rules={[{ required: true, message: `请输入${AddType}` }]}
                    >
                        <Input />
                    </Form.Item>
                    {
                        AddType == "新增使用人类型" && <Form.Item
                            label="简称"
                            name="short_name"
                            rules={[{ required: true, message: '请输入简称' }]}
                        >
                            <Input />
                        </Form.Item>
                    }





                </Form>
            </Modal>


        </div>
    )
}

export default AddModel